package com.bc004346.travelus;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;

import static com.bc004346.travelus.Utility.log;
import static com.bc004346.travelus.Utility.prepareAndSendEmail;

/**
 * Created by Renats on 2/28/14.
 * App About page
 */
public class AboutActivity extends Activity {

    private NavigationDrawerFragment mNavigationDrawer;

    //<editor-fold desc="Overrides">

    @Override
    protected void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        try {
            setAppVersionTextField();
            setupSendFeedbackButton();
            setupRateThisAppButton();

            mNavigationDrawer = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fullscreenDrawer);
            if (mNavigationDrawer != null)
                mNavigationDrawer.init();
        } catch (Exception e) {
            log(e, this);
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mNavigationDrawer.syncState();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        mNavigationDrawer.handleOnPrepareOptionsMenu(menu);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        mNavigationDrawer.syncState();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mNavigationDrawer.handleOnOptionsItemSelected(item);
        return super.onOptionsItemSelected(item);
    }

    //</editor-fold>

    //<editor-fold desc="Private Helpers">
    private void setAppVersionTextField() throws PackageManager.NameNotFoundException {
        String packageName = getPackageName();
        PackageManager pm = getPackageManager();
        if (pm != null) {
            TextView textView = (TextView) findViewById(R.id.aboutActivityTextViewAppName);
            textView.setText(String.format("%s\nVersion %s\n",
                    getString(R.string.app_name),
                    pm.getPackageInfo(packageName, 0).versionName));
        }
    }

    private void setupRateThisAppButton() {
        Button button = (Button) findViewById(R.id.aboutActivityButtonRateThisApp);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
            }
        });
    }

    private void setupSendFeedbackButton() {
        Button button = (Button) findViewById(R.id.aboutActivityButtonSubmitFeedback);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                prepareAndSendEmail(AboutActivity.this);
            }
        });
    }
    //</editor-fold>
}
